﻿using DevTeam;
using System;
using System.Data;

//MONTI INDIA
namespace ConfExpert.Forms.ImportWizards
{
    public partial class Monti_Tip2 : XITopCekiListesi
    {
        int YuklemeId;
        public Monti_Tip2(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            Init("Monti Tip2 Çeki Listesi",true);
            this.FileDone += Monti_Tip2_FileDone;
            //this.SplitterWidthPercentage = 30;
        }

        void Monti_Tip2_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    DataRow dr0 = dt.Rows[0];
                    string colName = dr0[colIndex].ToStr();
                    switch (colName)
                    {
                        case "Art": dt.Columns[colIndex].ColumnName = "Kalite"; break;
                        case "Fin": dt.Columns[colIndex].ColumnName = "Fin"; break;
                        case "Des": dt.Columns[colIndex].ColumnName = "DesenNo"; break;
                        case "Col": dt.Columns[colIndex].ColumnName = "Color"; break;
                        case "Roll": dt.Columns[colIndex].ColumnName = "VendorRollId"; break;
                        case "Roll Width  Cm.": dt.Columns[colIndex].ColumnName = "Width"; break;
                        case "Roll Lenght Mt.": dt.Columns[colIndex].ColumnName = "Qty"; break;
                        case "Customer Art.Ref.": dt.Columns[colIndex].ColumnName = "CustomerCode"; break;
                    }
                }

                if (dt.Rows[0]["Qty"].ToDecimal() == 0)
                    dt.Rows.RemoveAt(0);
                
                foreach (DataRow dr in dt.Rows)
                {
                    //string sheetName = dr["Article"].ToString();
                    string customerCode = dr["CustomerCode"].ToString();

                    try
                    {
                        if (Char.IsDigit(customerCode.ToCharArray()[customerCode.Length - 1]))
                        {
                            int index = customerCode.LastIndexOf(" ");
                            int length = customerCode.Length;

                            if (Char.IsDigit(customerCode.ToCharArray()[customerCode.Length - 2]))
                                customerCode = customerCode.Substring(0, index).Trim() + "/" + customerCode.Substring(index + 1, length - index - 1);
                            else
                                customerCode = customerCode.Substring(0, index).Trim() + "/" + customerCode.Substring(index + 1, length - index - 1).PadLeft(2, '0');
                        }
                    }
                    catch
                    { }

                    //string sheetName = dr["Kalite"].ToString().PadLeft(3, '0') + "-" + dr["DesenNo"].ToString().PadLeft(4, '0') + "/" + dr["Color"].ToString().PadLeft(3, '0');
                    string article = dr["Kalite"].ToString().PadLeft(3, '0') +
                        (dr["Fin"].ToString() != "0" ? "-" + dr["Fin"].ToString().PadLeft(2, '0') : string.Empty) +
                        "-" + dr["DesenNo"].ToString().PadLeft(4, '0') +
                        "/" + dr["Color"].ToString().PadLeft(3, '0');

                    if (customerCode != string.Empty)
                        article = customerCode; //CafeCoton için!

                    if (Article.Select("Article='" + article + "'").Length == 0)
                    {
                        DataRow drArticle = Article.NewRow();
                        drArticle["Article"] = article;
                        drArticle["CustomerCode"] = customerCode;
                        Article.Rows.Add(drArticle);
                    }
                    DataRow drRoll = Roll.NewRow();
                    drRoll["Article"] = article;
                    //drRoll["CustomerCode"] = customerCode;
                    drRoll["VendorRollId"] = dr["VendorRollId"];
                    drRoll["Qty"] = dr["Qty"];
                    drRoll["WidthUsable"] = dr["Width"];

                    Roll.Rows.Add(drRoll);
                }
            }

        }

    }
}


